﻿using GmSm.common;
using GmSm.Lib;
using GmSm.models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace GmSm.ScheduleTask
{
    /// <summary>
    /// 上传监管平台数据
    /// </summary>
    [DisallowConcurrentExecution]
    public class Hlwyy_Sdygzx : IJob
    {
        //修改此及Util类中配置
        public string platformId = "";//请提供platformId
        public string appSecret = "";//请提供appSecret

        public async Task Execute(IJobExecutionContext context)
        {
            await Console.Out.WriteLineAsync($"***************************Start******************************");
            await Console.Out.WriteLineAsync($"开始上传监管数据时间: {DateTime.Now}");

            try
            {
                //示例 2.2、医疗人员诊疗业务开通备案
                //StaffBusinessFiling();
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync($"上传监管数据出错: {ex.ToString()}");
                LogHelper.Message(ex.ToString());
            }

            await Console.Out.WriteLineAsync($"结束上传监管数据时间: {DateTime.Now}");
            await Console.Out.WriteLineAsync($"****************************End*******************************");
        }
        public void StaffBusinessFiling()
        {
            #region 2.2、医疗人员诊疗业务开通备案
            Filing_StaffBusinessFiling filing = new Filing_StaffBusinessFiling();
            filing.platformId = platformId;
            filing.staffId = "0350";
            filing.staffName = "曲海燕";
            filing.staffCertType = "1";
            filing.staffCertNo = "350802199003074396";
            filing.businessType = "20|30";
            filing.updateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            filing.filingMark = 1;
            string ns1 = JsonConvert.SerializeObject(filing);

            //国密加密
            SM4Utils sm4Utils = new SM4Utils();
            sm4Utils.secretKey = appSecret;
            string encStr = sm4Utils.Encrypt_ECBV2(ns1);

            Hashtable ht = new Hashtable();
            ht.Add("requestData", encStr);
            string outsm4 = Util.PostJson("filing", "staffBusinessFiling", JsonConvert.SerializeObject(ht)).Replace("\"", "");

            //国密解密
            string encStr2 = sm4Utils.Decrypt_ECBV2(outsm4);
            LogHelper.Message("解密数据："+ encStr2);
            string jsonco = encStr2.Replace("\r", "").Replace("\n", "");
            JObject jjsonbase641 = JObject.Parse(jsonco);
            if (jjsonbase641["code"]?.ToString() == "200")
            {
                Console.WriteLine("上传成功！");
            }
            else
            {
                Console.WriteLine(jjsonbase641["message"]?.ToString());
            }
            #endregion
        }
    }
}
